library(tidyverse) # for data cleaning and plotting
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.0.5 ✓ dplyr 1.0.3
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(gardenR) # for Lisa's garden data
library(lubridate) # for date manipulation
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(openintro) # for the abbr2state() function
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(ggmap) # for mapping points on maps
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(gplots) # for col2hex() function
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
library(leaflet) # for highly customizable mapping
library(ggthemes) # for more themes (including theme_map())
library(plotly) # for the ggplotly() - basic interactivity
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggmap':
##
## wind
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(gganimate) # for adding animation layers to ggplots
library(transformr) # for "tweening" (gganimate)
##
## Attaching package: 'transformr'
## The following object is masked from 'package:sf':
##
## st_normalize
library(gifski) # need the library for creating gifs but don't need to load each time
library(shiny) # for creating interactive apps
library(ggimage)
##
## Attaching package: 'ggimage'
## The following object is masked from 'package:ggmap':
##
## theme_nothing
# SNCF Train data
small_trains <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/small_trains.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## year = col_double(),
## month = col_double(),
## service = col_character(),
## departure_station = col_character(),
## arrival_station = col_character(),
## journey_time_avg = col_double(),
## total_num_trips = col_double(),
## avg_delay_all_departing = col_double(),
## avg_delay_all_arriving = col_double(),
## num_late_at_departure = col_double(),
## num_arriving_late = col_double(),
## delay_cause = col_character(),
## delayed_number = col_double()
## )
# Lisa's garden data
data("garden_harvest")
# Lisa's Mallorca cycling data
mallorca_bike_day7 <- read_csv("https://www.dropbox.com/s/zc6jan4ltmjtvy0/mallorca_bike_day7.csv?dl=1") %>%
select(1:4, speed)
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## lon = col_double(),
## lat = col_double(),
## ele = col_double(),
## time = col_datetime(format = ""),
## extensions = col_double(),
## ele.num = col_double(),
## date = col_date(format = ""),
## hrminsec = col_datetime(format = ""),
## time_hr = col_double(),
## dist_km = col_double(),
## speed = col_double()
## )
# Heather Lendway's Ironman 70.3 Pan Am championships Panama data
panama_swim <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_swim_20160131.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## lon = col_double(),
## lat = col_double(),
## time = col_datetime(format = ""),
## extensions = col_double(),
## ele = col_logical(),
## event = col_character(),
## date = col_date(format = ""),
## hrminsec = col_datetime(format = "")
## )
panama_bike <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_bike_20160131.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## lon = col_double(),
## lat = col_double(),
## ele = col_double(),
## time = col_datetime(format = ""),
## extensions = col_double(),
## event = col_character(),
## date = col_date(format = ""),
## hrminsec = col_datetime(format = "")
## )
panama_run <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_run_20160131.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## lon = col_double(),
## lat = col_double(),
## ele = col_double(),
## time = col_datetime(format = ""),
## extensions = col_double(),
## event = col_character(),
## date = col_date(format = ""),
## hrminsec = col_datetime(format = "")
## )
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## date = col_date(format = ""),
## state = col_character(),
## fips = col_character(),
## cases = col_double(),
## deaths = col_double()
## )
For ALL graphs, you should include appropriate labels.
NEW!! With animated graphs, add eval=FALSE to the code chunk that creates the animation and saves it using anim_save(). Add another code chunk to reread the gif back into the file. See the tutorial for help.
ggplotly() function.covid_cumcase <-covid19 %>%
filter(state %in% c("Minnesota", "Wisconsin", "Iowa", "North Dakota", "South Dakota")) %>%
ggplot(aes(x = date, y = cases, color = state)) +
geom_line() +
labs(x = "",
y = "",
color = "",
title = "Cumulative COVID-19 cases") +
theme(legend.position = "bottom") +
scale_y_continuous(labels = scales::comma) +
scale_x_date(date_breaks = "1 month",
date_labels = "%b",
date_minor_breaks = "1 week")
ggplotly(covid_cumcase)
lettuce_variety <- garden_harvest %>%
filter(vegetable == "lettuce") %>%
ggplot(aes(y = fct_rev(fct_infreq(variety)))) +
geom_bar() +
labs(title = "Number of times lettuce \n varieties were harvested",
x = "",
y = "")
ggplotly(lettuce_variety)
small_trains dataset that contains data from the SNCF (National Society of French Railways). These are Tidy Tuesday data! Read more about it here.trains_journey <- small_trains %>%
ggplot(aes(x = journey_time_avg, y = num_arriving_late)) +
geom_point(alpha = .5) +
theme(axis.text.y = element_blank(),
plot.title = element_text(face = "bold", hjust = .5),
axis.text = element_text(color = "black", face = "italic")) +
labs(title = "Train journey average by the total number of trains arriving late over the years",
x = "Journey time average",
y = "Number of trains arriving late") +
facet_wrap(vars(year))
trains_journey +
transition_time(journey_time_avg)
anim_save("journeys.gif")
knitr::include_graphics("journeys.gif")
geom_area() examples here). You will look at cumulative harvest of tomato varieties over time. You should do the following:garden_harvest data, filter the data to the tomatoes and find the daily harvest in pounds for each variety.fct_reorder()) from most to least harvested (most on the bottom).tomatoes_cum <- garden_harvest %>%
filter(vegetable == "tomatoes") %>%
group_by(date, variety) %>%
summarize(daily_harvest_lbs = sum(weight)*0.00220462) %>%
ungroup() %>%
complete(variety, date, fill = list(daily_harvest_lbs = 0)) %>%
group_by(variety) %>%
mutate(cum_wt_lbs = cumsum(daily_harvest_lbs)) %>%
ggplot(aes(x = date, y = cum_wt_lbs, fill = fct_reorder(variety, cum_wt_lbs, max))) +
geom_area() +
labs(title = "Cumulative Tomato Harvest by variety (lbs)",
fill = "Varieties",
x = "",
y = "") +
theme(plot.title = element_text(face = "bold", hjust = .5))
tomatoes_cum +
transition_reveal(date)
anim_save("tomato_varieties.gif")
knitr::include_graphics("tomato_varieties.gif")
mallorca_bike_day7 bike ride using animation! Requirements:ggmap.ggimage package and geom_image to add a bike image instead of a red point. You can use this image. See here for an example.I prefer this animated map because it is more dynamic and gives more of an idea of how your bike ride went.
bike_image_link <- "https://raw.githubusercontent.com/llendway/animation_and_interactivity/master/bike.png"
mallorca_map <- get_stamenmap(
bbox = c(left = 2.28, bottom = 39.41, right = 3.03, top = 39.8),
maptype = "terrain",
zoom = 11)
bikeride <- ggmap(mallorca_map) +
geom_path(data = mallorca_bike_day7,
aes(x = lon, y = lat, color = ele, size = speed)) +
scale_color_viridis_c(option = "magma") +
geom_image(data = mallorca_bike_day7 %>%
mutate(picture = bike_image_link),
aes(x = lon, y = lat, image = picture),
size = 0.07) +
labs(title = "Lisa's Mallorca bike ride",
subtitle = "Time : {frame_along}",
x = "",
y = "") +
theme_map() +
theme(plot.title = element_text(face = "bold", hjust = .5, size = 20),
plot.subtitle = element_text(face = "bold", hjust = .5, size = 15))
bikeride +
transition_reveal(time)
anim_save("lisa_ride.gif")
knitr::include_graphics("lisa_ride.gif")
panama_swim, panama_bike, and panama_run. Create a similar map to the one you created with my cycling data. You will need to make some small changes:bind_rows(),panama_all <- bind_rows(panama_swim, panama_bike, panama_run)
Panama <- get_stamenmap(
bbox = c(left = -79.8420, bottom = 8.7394, right = -79.2034, top = 9.1159),
maptype = "terrain",
zoom = 10)
heather_ironman <- ggmap(Panama) +
geom_path(data = panama_all,
aes(x = lon, y = lat),
size = 1) +
geom_point(data = panama_all,
aes(x = lon, y = lat, color = event),
size = 3) +
labs(title = "Heather's Panama triatholon",
x = "",
y = "",
color = "Event") +
theme(plot.title = element_text(face = "bold", hjust = .5)) +
theme_map()
heather_ironman +
transition_reveal(hrminsec)
anim_save("ironman.gif")
knitr::include_graphics("ironman.gif")